﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;

namespace DAL
{
    public class DepartmentType
    {
        public static void insertAsParent(string department)
        {
            DbHelper.ExecuteNonQuery("p_Department_insertWithparent", department);
        }

        public static void update(int dId, string department,int parentId,int sort)
        {
            DbHelper.ExecuteNonQuery("p_Department_update", dId, department,parentId,sort);
        }

        public static void deleteWithParent(int dId)
        {
            DbHelper.ExecuteNonQuery("p_Department_deleteWithParent", dId);
        }

        public static void insertAsChild(int dId, string department,int sort)
        {
            DbHelper.ExecuteNonQuery("p_Department_insertWithchild", dId, department,sort);
        }

        public static void deleteWithChild(int dId)
        {
            DbHelper.ExecuteNonQuery("p_DepartmentType_deleteWithChild", dId);
        }
        public static DataTable getByParent(int parentId)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_DepartmentType_getByParent]", parentId);
        }
        public static DepartmentTypeEntity getById(int dId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("[dbo].[p_DepartmentType_getById]", dId);
            DepartmentTypeEntity dtEnt = new DepartmentTypeEntity();
            if (dr != null)
            {
                dtEnt.DepartmentType = dr["department"].ToString();
                dtEnt.ParentId = Convert.ToInt32(dr["parentId"]);
                dtEnt.Sort = Convert.ToInt32(dr["sort"]);
                dtEnt.IsShow= Convert.ToBoolean(dr["isShow"]);
            }
            return dtEnt;
        }

        public static DepartmentTypeEntity getByChildId(int dId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_DepartmentTpye_getByChildId", dId);
            DepartmentTypeEntity dtEnt = new DepartmentTypeEntity();
            dtEnt.IsShow = true;
            if (dr != null)
            {
                dtEnt.DepartmentType = dr["department"].ToString();
                dtEnt.ParentId = Convert.ToInt32(dr["parentId"]);
                dtEnt.Sort = Convert.ToInt32(dr["sort"]);
                dtEnt.IsShow = Convert.ToBoolean(dr["isShow"]);
            }
            return dtEnt;
        }

        public static DataTable listWithParent()
        {
            return DbHelper.ExecuteDataTable("p_Department_selectByParent");
        }
        public static DataTable listWithAll()
        {
            return DbHelper.ExecuteDataTable("p_Department_selectAll");
        }

        public static DataTable ChildrenDtNum(int parentId)
        {
            return DbHelper.ExecuteDataTable("p_DepartmentType_ChildrenNum", parentId);
        }

        public static DataTable selectWithComplaintTypeId(int complaintId)
        {
            return DbHelper.ExecuteDataTable("P_Complaint_getDepartmentId", complaintId);
        }

        public static void updateIsShow(int dId)
        {
            DbHelper.ExecuteNonQuery("p_DepartmentType_updateIsShow", dId);
        }
    }
}
